<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Overtrue\Wechat\Media;

class NewsController extends Controller
{
    /**
     * 获取图文列表
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $mediaIdList = DB::table('news')->groupby('media_id')->orderby('update_time','desc')->paginate(10);
//        dd($mediaIdList);
        foreach($mediaIdList as $key=>$value) {
            if($key == 0) {
                unset($mediaIdList['data']);
            }
            // 根据mediaid获取图文信息组装每个图文数组
            $newsInfo = $this->getNewsBymediaId($value->media_id);
            foreach($newsInfo as $k=>$newitem) {
                $mediaIdList2['data'][$key][$k]['id'] = $newitem->id;
                $mediaIdList2['data'][$key][$k]['media_id'] = $newitem->media_id;
                $mediaIdList2['data'][$key][$k]['title'] = $newitem->title;
                $mediaIdList2['data'][$key][$k]['author'] = $newitem->author;
                $mediaIdList2['data'][$key][$k]['digest'] = $newitem->digest;
                $mediaIdList2['data'][$key][$k]['content'] = $newitem->content;
                $mediaIdList2['data'][$key][$k]['content_source_url'] = $newitem->content_source_url;
                $mediaIdList2['data'][$key][$k]['thumb_media_id'] = $newitem->thumb_media_id;
                $thumb_pic = DB::table('picuploads')->where('media_id', $newitem->thumb_media_id)->lists('weixinurl');
                $mediaIdList2['data'][$key][$k]['thumb_pic'] = $thumb_pic[0];
                $mediaIdList2['data'][$key][$k]['show_cover_pic'] = $newitem->show_cover_pic;
                $mediaIdList2['data'][$key][$k]['url'] = $newitem->url;
                $mediaIdList2['data'][$key][$k]['update_time'] = $newitem->update_time;
            }
        }

        // 找到最新图文信息
        $first_news = DB::table('news')->groupby('media_id')->orderby('update_time','desc')->first();
        $first_newsInfo = $this->getNewsBymediaId($first_news->media_id);
        foreach($first_newsInfo as $k=>$first_item) {
            $mediaIdList3['data'][$k]['id'] = $first_item->id;
            $mediaIdList3['data'][$k]['media_id'] = $first_item->media_id;
            $mediaIdList3['data'][$k]['title'] = $first_item->title;
            $mediaIdList3['data'][$k]['author'] = $first_item->author;
            $mediaIdList3['data'][$k]['digest'] = $first_item->digest;
            $mediaIdList3['data'][$k]['content'] = $first_item->content;
            $mediaIdList3['data'][$k]['content_source_url'] = $first_item->content_source_url;
            $mediaIdList3['data'][$k]['thumb_media_id'] = $first_item->thumb_media_id;
            $thumb_pic = DB::table('picuploads')->where('media_id', $first_item->thumb_media_id)->lists('weixinurl');
            $mediaIdList3['data'][$k]['thumb_pic'] = $thumb_pic[0];
            $mediaIdList3['data'][$k]['show_cover_pic'] = $first_item->show_cover_pic;
            $mediaIdList3['data'][$k]['url'] = $first_item->url;
            $mediaIdList3['data'][$k]['update_time'] = $first_item->update_time;
        }

        $new_data['new_news'] = $mediaIdList3['data'];
        $new_data['page'] = $mediaIdList;
        $new_data['mediaIdList'] = $mediaIdList2['data'];
        //dd($new_data['page']->hasMorePages());
//dd($new_data);

        if(is_pjax()) {
            return View('admin.newslistsegment', $new_data);
        }

        return View('admin.newslist', $new_data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
        $data = [];
        $data['edit_mode'] = 0;
        if(is_pjax()) {
            return View('admin.addnewssegment', $data);
        }
        return View('admin.addnews', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Media $media)
    {
        //
        $mediaId = \Input::get('mediaid');

        $result = $media->delete($mediaId);

        if(isset($result['errcode']) && $result['errcode'] == 0) {
            // 删除本地图文
            DB::table('news')->where('media_id', $mediaId)->delete();
            return response()->json(['code'=>2000, 'message'=> '删除图文成功！']);
        }else {
            return response()->json(['code'=>2100, 'message'=> '删除图文失败！']);
        }
    }

    /**
     * 根据mediaid 获取图文信息
     * @param $mediaid
     */
    private function getNewsBymediaId($mediaid) {
        return DB::table('news')->where('media_id', $mediaid)->orderby('id', 'asc')->get();
    }
}
